import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from fake_useragent import UserAgent


def time_watcher():
    print('--- %s seconds ---' % (time.time() - start_time))


def get_data(browser):
    try:
        ip = browser.find_element_by_class_name("ip-block")
        ip_text = ip.find_element_by_class_name('ip').text.strip()
    except:
        ip = 'Not found'
    try:
        agent = browser.find_element_by_xpath(
            '/html/body/div[4]/table/tbody/tr/td[2]/div/div[2]/span[2]')
        agent_text = agent.text.strip()
    except:
        agent = 'Not found'

    data = {'ip': ip_text, 'agent': agent_text}

    print(data)
    time.sleep(20)


def setup_browser(proxy=None):

    # подмена UserAgent
    ua = UserAgent()
    # user_agent = ua.random
    user_agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36'

    # установка proxy
    if proxy != None:
        PROXY = proxy
        webdriver.DesiredCapabilities.CHROME['proxy'] = {
            "httpProxy": PROXY,
            "ftpProxy": PROXY,
            "sslProxy": PROXY,
            "proxyType": "MANUAL"}

    options = webdriver.ChromeOptions()
    options.add_argument(f"--user-agent={user_agent}")
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome(chrome_options=options,
                              executable_path=ChromeDriverManager().install())

    return driver


def main():
    url = 'http://sitespy.ru/my-ip'
    proxy = 'http://175.106.18.201:38708'

    browser = setup_browser()
    browser.get(url)
    get_data(browser)


if __name__ == '__main__':
    start_time = time.time()
    main()
    time_watcher()
